# encoding: utf-8
from pyo import *

class Audio_Server:
    def __init__ (self):
        self.sndProcessing = []
        self.s= Server(sr=44100, buffersize=512, duplex=1)
        self.getAudioDrivers()
        self.s.setOutputDevice(self.defaultOutputIndex)
        
        self.s.boot()
        self.s.start()

    def start(self):
        self.s.start()

    def stop(self):
        if self.s.getIsStarted():
            self.s.stop()

    def setOut(self):
        self.output.out()
        return self

    def getSndProcessing(self):
        return self.sndProcessing

   
    def addSndProcessing(self, sndProcessing):#a verifier
        if sndProcessing == "delay":
            self.sndProcessing.append(MFX().out())
        elif sndProcessing == "distortion":
            self.sndProcessing.append(MFX().out())
        elif sndProcessing== "frequency shift":
            self.Processing.append(MFX().out())
        return self.sndProcessing[-1]

    def changeAudioDriver(self, driver): #***
        pos = self.outputDriverNames.index(driver)
        self.defaultOutputIndex = self.outputDriverIndexes[pos]
        self.stop()
        if self.s.getIsBooted():
            self.s.shutdown()
        self.s.setOutputDevice(self.defaultOutputIndex)
        self.s.boot()

        
    def getAudioDrivers(self): #\\\
        self.outputDriverNames, self.outputDriverIndexes = pa_get_output_devices()
        #print self.outputDriverNames, self.outputIndexes
        self.defaultOutputIndex = pa_get_default_output()
        pos = self.outputDriverIndexes.index(self.defaultOutputIndex)
        self.defaultOutputName = self.outputDriverNames[pos]
        

    def getAudioDriverNames(self):
        return self.outputDriverNames, self.defaultOutputName
        #print self.outputDriverNames, self.defaultOutputName

class MFX(Audio_Server):

    def __init__(self):
        Audio_Server.__init__(self)
        self._in=Input(chnl=0, mul=1, add=0)
        self._mix= Mixer (outs= 3, chnls=2, time=.05)
        self._dly = Delay(self._mix[0], delay=0.78, feedback=0.5, maxdelay=1, mul=1, add=0)
        self._dis= Disto(self._mix[1], drive=0.75, slope=0.50, mul=1, add=0)
        self._frs= FreqShift(self._mix[2], shift=100, mul=1, add=0)
        self._mix.addInput(0, self._in)
        self._mix.addInput(1, self._frs)
        self._mix.addInput(2, self._dly)
        self._mix.addInput(3, self._dis)
        #self._mix.addInput(4, self._grain)

        self.output= self._dly + self._dis + self._frs

    def setAmp(self, _ins, _outs, _vol):
        self._mix.setAmp(_ins, _outs, _vol)
 
